// 林天浩诸神黄昏游戏
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const url = 'https://www.cnshxs.net/xs/58355/';
module.exports = async () => {
    // 打开浏览器
    const browser = await puppeteer.launch({
        args: ['--no-sandbox'],
        headless: 'new'
    });
    // 创建tab标签页
    const page = await browser.newPage();

    // 地址栏输入网址
    await page.goto(url, {
        waitUntil: 'networkidle2', // 等待网络状态为空闲的时候才继续执行
    });
    // 等待网页元素加载完成
    await timeout();
    let catalogueList = await page.evaluate(() => {
        let catalogue = [];
        const ddList = document.querySelector('#list').children[0].children;
        for(let i = 0; i < ddList.length; i++) {
            if (i >= 11){
                const href = ddList[i].children[0].getAttribute('href');
                const link = href.split('/')[href.split('/').length - 1];
                const title = ddList[i].children[0].innerText;
                catalogue.push({
                    title,
                    link: 'https://www.cnshxs.net/xs/58355/' + link
                });
            }
        }
        // 返回爬取到的数据
        return catalogue;
    });
    const contentList = [];
    for (let i = 0; i <= 863; i++) {
        await page.goto(catalogueList[i].link, {
            waitUntil: 'networkidle2', // 等待网络状态为空闲的时候才继续执行
        });
        const data = await page.evaluate(() => {
            const title = document.querySelector('.bookname h1').innerText;
            const content = document.querySelector('#content').innerText;
            return {
                title,
                content
            }
        });
        contentList.push(data);
    }
    console.log('获取到所有内容数据:' + contentList.length);
    for (let i = 0; i < contentList.length; i++) {
        console.log('正在写入：' + contentList[i].title);
        fs.appendFileSync('./ebookSjjx.txt', `${contentList[i].title}\n${contentList[i].content}\n\n\n`,  function (err) {
            if (err) {
                console.log(err);
                return;
            }
            console.log('写入成功：' + contentList[i].title);
        });
    }
    // 关闭浏览器
    await browser.close();
}

function timeout() {
    return new Promise(resolve => setTimeout(resolve, 4000))
}
